package com.tianjipms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tianjipms.dao.RepairMapper;
import com.tianjipms.dto.UserRepairDTO;
import com.tianjipms.entity.Repair;
import com.tianjipms.service.RepairService;
import com.tianjipms.utils.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

@Service
@Transactional
public class RepairServiceImpl extends ServiceImpl<RepairMapper, Repair> implements RepairService {
    @Resource
    private RepairMapper repairMapper;
    @Override
    public IPage<Repair> findRepairListByUserIdPage(IPage<Repair> page, Long userId) {
        QueryWrapper<Repair> queryWrapper = new QueryWrapper<Repair>();
        queryWrapper.eq("user_id", userId);
        return repairMapper.selectPage(page, queryWrapper);
    }

    @Override
    public IPage<Repair> findRepairListByServiceIdPage(IPage<Repair> page, Long serviceId) {
        QueryWrapper<Repair> queryWrapper = new QueryWrapper<Repair>();
        queryWrapper.eq("service_id", serviceId);
        return repairMapper.selectPage(page, queryWrapper);
    }


    @Override
    public Result updateByRepair(Repair repair) {
        Repair updateRepair = repairMapper.selectById(repair.getId());
        if(!ObjectUtils.isEmpty(repair.getUserId())){
            updateRepair.setUserId(repair.getUserId());
        }
        if(!ObjectUtils.isEmpty(repair.getType())){
            updateRepair.setType(repair.getType());
        }
        if(!ObjectUtils.isEmpty(repair.getServiceId())){
            updateRepair.setServiceId(repair.getServiceId());
        }
        if(!ObjectUtils.isEmpty(repair.getMessage())){
            updateRepair.setMessage(repair.getMessage());
        }
        if(!ObjectUtils.isEmpty(repair.getServiceTime())){
            updateRepair.setServiceTime(repair.getServiceTime());
        }
        if(!ObjectUtils.isEmpty(repair.getServiceStatus())){
            updateRepair.setServiceStatus(repair.getServiceStatus());
        }
        if(!ObjectUtils.isEmpty(repair.getUserStatus())){
            updateRepair.setUserStatus(repair.getUserStatus());
        }
        if(!ObjectUtils.isEmpty(repair.getPrice())){
            updateRepair.setPrice(repair.getPrice());
        }

        repairMapper.updateById(updateRepair);
        return Result.ok();
    }

    @Override
    public Result updateServiceTimeByRepair(Repair repair) {
        Repair updateRepair = repairMapper.selectById(repair.getId());
        if(!ObjectUtils.isEmpty(repair.getUserId())){
            updateRepair.setUserId(repair.getUserId());
        }
        if(!ObjectUtils.isEmpty(repair.getType())){
            updateRepair.setType(repair.getType());
        }
        if(!ObjectUtils.isEmpty(repair.getServiceId())){
            updateRepair.setServiceId(repair.getServiceId());
        }
        if(!ObjectUtils.isEmpty(repair.getMessage())){
            updateRepair.setMessage(repair.getMessage());
        }
        if(!ObjectUtils.isEmpty(repair.getServiceStatus())){
            updateRepair.setServiceStatus(repair.getServiceStatus());
        }
        if(!ObjectUtils.isEmpty(repair.getUserStatus())){
            updateRepair.setUserStatus(repair.getUserStatus());
        }
        if(!ObjectUtils.isEmpty(repair.getPrice())){
            updateRepair.setPrice(repair.getPrice());
        }
        updateRepair.setServiceTime(repair.getServiceTime());
        repairMapper.updateById(updateRepair);
        return Result.ok();
    }


}
